package com.lplb.common.core.utils;

import java.util.regex.Pattern;

/**
 * 格式验证工具（手机号正则表达式）
 *
 * @author wbg
 * @Time 2019-11-29 15:25
 */
public class RegexVerifyUtil {

    /**不严谨的表达式="/^[1]([3-9])[0-9]{9}$/"* */
    public static final String MOBILE="^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$";
    public static final String EMAIL="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    public static final String PASSWORD="^(?=.*\\d)(?=.*[A-Za-z])[\\x20-\\x7e]{6,32}$";
    public static Boolean verifyMobile(String phone){
        return Pattern.compile(MOBILE).matcher(phone).matches();
    }
    public static Boolean verifyEmail(String email){
        return Pattern.compile(EMAIL).matcher(email).matches();
    }
    public static Boolean verifyPassWord(String password){
        return Pattern.compile(PASSWORD).matcher(password).matches();
    }
    public static Boolean verify(String pattern,String info){
       return Pattern.compile(pattern).matcher(info).matches();
    }

    /**
     * 匹配Luhn算法：可用于检测银行卡卡号
     * @param cardNo
     * @return
     */
    public static boolean verifyCardNo(String cardNo) {
        int[] cardNoArr = new int[cardNo.length()];
        for (int i=0; i<cardNo.length(); i++) {
            cardNoArr[i] = Integer.valueOf(String.valueOf(cardNo.charAt(i)));
        }
        for(int i=cardNoArr.length-2;i>=0;i-=2) {
            cardNoArr[i] <<= 1;
            cardNoArr[i] = cardNoArr[i]/10 + cardNoArr[i]%10;
        }
        int sum = 0;
        for(int i=0;i<cardNoArr.length;i++) {
            sum += cardNoArr[i];
        }
        return sum % 10 == 0;
    }
}

